﻿package com.edward.action;

import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import org.apache.struts2.ServletActionContext;
import org.springframework.security.annotation.Secured;
import org.springframework.security.context.SecurityContextHolder;
import org.springframework.security.userdetails.UserDetails;

import com.edward.JsonDateProcessor;
import com.edward.PagerModel;
import com.edward.model.Cargo;
import com.edward.model.InventoryList;
import com.edward.model.Item;
import com.edward.service.CargoManager;
import com.edward.service.InventoryListManager;
import com.edward.service.ItemManager;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class InventoryListAction extends ActionSupport {


	private static final long serialVersionUID = 5332856839059165665L;

	private InventoryListManager inventoryListManager;
	
	private ItemManager itemManager;

	private CargoManager cargoManager;
	
	private InventoryList inventoryList;
	
	private Item item;
	
	private Cargo cargo;
	
	private int id;
	
	private int limit;
	
	private int start;
	
	private String query;
	
	private boolean oversea;


	public int getId() {
		return id;
	}


	public void setId(int id) {
		this.id = id;
	}
	

	public boolean isOversea() {
		return oversea;
	}


	public void setOversea(boolean oversea) {
		this.oversea = oversea;
	}


	public InventoryList getInventoryList() {
		return inventoryList;
	}


	public void setInventoryList(InventoryList inventoryList) {
		this.inventoryList = inventoryList;
	}


	public Item getItem() {
		return item;
	}


	public void setItem(Item item) {
		this.item = item;
	}


	public void setInventoryListManager(InventoryListManager inventoryListManager) {
		this.inventoryListManager = inventoryListManager;
	}


	public void setItemManager(ItemManager itemManager) {
		this.itemManager = itemManager;
	}


	public Cargo getCargo() {
		return cargo;
	}


	public void setCargo(Cargo cargo) {
		this.cargo = cargo;
	}


	public void setCargoManager(CargoManager cargoManager) {
		this.cargoManager = cargoManager;
	}
	
	public int getLimit() {
		return limit;
	}


	public void setLimit(int limit) {
		this.limit = limit;
	}


	public int getStart() {
		return start;
	}


	public void setStart(int start) {
		this.start = start;
	}
	
	public String getQuery() {
		return query;
	}


	public void setQuery(String query) {
		this.query = query;
	}

	
	//添加inventoryList 的表头
	@Secured("ROLE_DEPOT_ADD")
	public String add() throws Exception{
		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		UserDetails userDetails = (UserDetails) SecurityContextHolder
		.getContext().getAuthentication().getPrincipal();
		inventoryList.setAuditor(inventoryListManager.getUserName(userDetails.getUsername()));
		inventoryListManager.addInventoryList(inventoryList);
		int total = inventoryListManager.listInventoryList(oversea).size();
		response.getWriter().print("{success : true , msg : '添加成功!',inventoryList_id:"+ inventoryList.getId()+",total:" + total + "}");
		return null;
	}
	
	@Secured("ROLE_DEPOT_UPDATE")
	public String update() throws Exception{
		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		InventoryList s = inventoryListManager.getInventoryList(inventoryList.getId());
		inventoryList.setItems(s.getItems());
		inventoryList.setAuditor(s.getAuditor());
		inventoryListManager.updateInventoryList(inventoryList);
		response.getWriter().print("{success : true , msg : '修改成功!'}");
		return null;
	}
	
	@Secured("ROLE_DEPOT_UPDATE")
	public String delete() throws Exception{
		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		InventoryList s = inventoryListManager.getInventoryList(inventoryList.getId());
		if(s.getItems().size() != 0){
			response.getWriter().print("{success : false , msg : '删除失败，存在销售货物列表，请先删除销售货物!'}");
		}else{
			inventoryListManager.deleteInventoryList(s.getId());
			response.getWriter().print("{success : true , msg : '删除成功!'}");
		}
		return null;
	}

	@Secured("ROLE_DEPOT")
	public String getList() throws Exception {

		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
	    JsonConfig cfg = new JsonConfig();
		cfg.registerJsonValueProcessor(java.util.Date.class, new JsonDateProcessor());
		cfg.registerJsonValueProcessor(java.sql.Date.class, new JsonDateProcessor());
		PagerModel pager;
		pager = inventoryListManager.pagerList(start, limit,oversea);
		JSONObject jsonObject = JSONObject.fromObject(pager,cfg);
		String jsonStr = jsonObject.toString();
		response.getWriter().print(jsonStr);
		return null;
	}
	
	
	
	
//	添加inventoryList 的 货物明细
	@Secured("ROLE_DEPOT_ADD")
	public String addItem() throws Exception{
		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		InventoryList s = inventoryListManager.getInventoryList(inventoryList.getId());
		Cargo c = cargoManager.getCargo(cargo.getId());
		item.setCount(c.getCount());
		c.setCount(item.getCheckCount());
		cargoManager.updateCargo(c);
		item.setCargo(c);
		item.setPlCount(item.getCheckCount()-item.getCount());
		item.setSum(item.getCount() * c.getPrice());
		item.setCheckSum(item.getCheckCount() * c.getPrice());
		item.setPlSum(item.getCheckSum() - item.getSum());
		itemManager.addItem(item);
		s.getItems().add(item);
		inventoryListManager.updateInventoryList(s);
		response.getWriter().print("{success : true , msg : '添加成功!'}");
		return null;
	}
	
	@Secured("ROLE_DEPOT_UPDATE")
	public String updateItem() throws Exception {

		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		Item i = itemManager.getItem(item.getId());
		//由于变更的cargo没有Id ,先从数据库中获取其ID 再一同更新
		Cargo c =i.getCargo();
		c.setCount(item.getCheckCount());
		cargoManager.updateCargo(c);
		i.setCheckCount(item.getCheckCount());
		i.setPlCount(item.getCheckCount()-item.getCount());
		i.setCheckSum(item.getCheckCount() * c.getPrice());
		i.setPlSum(item.getCheckSum() - item.getSum());
		itemManager.updateItem(i);
		response.getWriter().print("{success : true , msg : '修改成功!'}");
		return null;
	}
	
	@Secured("ROLE_DEPOT_UPDATE")
	public String deleteItem() throws Exception {

		ActionContext ctx = ActionContext.getContext();        
		  HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		Item i = itemManager.getItem(item.getId());
		Cargo c =i.getCargo();
		c.setCount(i.getCount());
		cargoManager.updateCargo(c);
		itemManager.deleteItem(item.getId());
		response.getWriter().print("{success : true , msg : '删除成功!'}");
		return null;
	}

	//获取grid 显示 的 分页信息
	@Secured("ROLE_DEPOT")
	public String getItems() throws Exception {

		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		JsonConfig cfg = new JsonConfig();
		cfg.registerJsonValueProcessor(java.util.Date.class, new JsonDateProcessor());
		cfg.registerJsonValueProcessor(java.sql.Date.class, new JsonDateProcessor());
		PagerModel pager;
	    pager = itemManager.pagerItem(start, limit,inventoryList.getId());
		JSONObject jsonObject = JSONObject.fromObject(pager,cfg);
		String jsonStr = jsonObject.toString();
		response.getWriter().print(jsonStr);
		return null;
	}
	
	@Secured("ROLE_DEPOT")
	 public String getListJudge() throws Exception {
			ActionContext ctx = ActionContext.getContext();        
			HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
			response.setContentType("text/html;charset=UTF-8");
				response.getWriter().print("{success : true , msg : '成功!',deny:false}");
		 return null;
	 }
	
}
